package com.chenfy.touchtransfer_android.tmp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pGroup;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.util.Log;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by ChenFengyao
 * Date: 20-11-10
 */
public class Wifip2pReceiver extends BroadcastReceiver {
    public WifiP2pManager mWifiP2pManager;
    public WifiP2pManager.Channel mChannel;
    public Wifip2pActionListener mListener;
    private List<WifiP2pDevice> list;

    public Wifip2pReceiver(WifiP2pManager wifiP2pManager, WifiP2pManager.Channel channel,
                           Wifip2pActionListener listener) {
        mWifiP2pManager = wifiP2pManager;
        mChannel = channel;
        mListener = listener;
        list = new ArrayList<WifiP2pDevice>();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("Wifip2pReceiver", intent.getAction());
        Log.d("Wifip2pReceiver", "intent:" + intent);
        switch (intent.getAction()){
            case WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION:
                boolean p2pIsEnable = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE,WifiP2pManager.WIFI_P2P_STATE_DISABLED) == WifiP2pManager.WIFI_P2P_STATE_ENABLED;
                Log.d("Wifip2pReceiver", "p2pIsEnable:" + p2pIsEnable);
                break;
            case WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION:
                //获取到设备列表信息
                WifiP2pDeviceList mPeers = intent.getParcelableExtra(WifiP2pManager.EXTRA_P2P_DEVICE_LIST);
                list.clear(); //清除旧的信息
                list.addAll(mPeers.getDeviceList()); //更新信息 这个是直连的数据
                for (WifiP2pDevice wifiP2pDevice : list) {
                    Log.d("Wifip2pReceiver", wifiP2pDevice.deviceName);
                }
//                adapter.notifyDataSetChanged();  //更新列表
                break;
            case WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION:
                NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
                WifiP2pInfo wifiP2pInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_INFO);
                WifiP2pGroup wifiP2pGroup = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_GROUP);
                break;
            case WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION:
                WifiP2pDevice device = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
                Log.d("Wifip2pReceiver", String.valueOf(device.deviceName));
//                deviceInfo.setText(getString(R.string.device_info) + device.deviceName );
                break;
            case WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION:
                int discoveryState = intent.getIntExtra(WifiP2pManager.EXTRA_DISCOVERY_STATE,WifiP2pManager.WIFI_P2P_DISCOVERY_STOPPED);
                Log.d("Wifip2pReceiver", "discoveryState:" + discoveryState);
//                isDiscover = discoveryState == WifiP2pManager.WIFI_P2P_DISCOVERY_STARTED;
//                updateState();
                break;
        }
    }

    // 获取直连的数据列表
    public List<WifiP2pDevice> getList() {
        return list;
    }
}
